package stack.be.touch.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import net.sf.stackwrap4j.entities.Question;


import stack.be.touch.dto.SiteDTO;
import stack.be.touch.ui.PickSitesActivity;

import stack.be.touch.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class QuestionsPageAdapter extends BaseAdapter {
	
	private Context ctx;
	private List<Question> questions;
	private OnClickListener tagClickListener;
	private LayoutInflater inflater;
	private Resources resources;
	private LinearLayout.LayoutParams tagLayout;

	
	
	public QuestionsPageAdapter(Context ctx, List<Question> questions,
			OnClickListener tagClicked) {
		this.ctx = ctx;
		this.questions = questions;
		this.tagClickListener = tagClicked;
		tagLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		tagLayout.setMargins(0, 0, 5, 0);	
		resources = ctx.getResources();
		inflater = LayoutInflater.from(ctx);
	}
	
	private static class ViewHolder {
		protected TextView title;
		protected TextView votes;
		protected TextView votesLabel;
		protected TextView answers;
		protected TextView answersLabel;
		protected TextView views;
		protected TextView viewsLabel;
		protected TextView bounty;
		protected LinearLayout tags;
		
		public ViewHolder(View v) {
			title = (TextView)v.findViewById(R.id.title);
			votes = (TextView)v.findViewById(R.id.votesN);
			votesLabel = (TextView)v.findViewById(R.id.votesL);
			answers = (TextView)v.findViewById(R.id.answersN);
			answersLabel = (TextView)v.findViewById(R.id.answersL);
			views = (TextView)v.findViewById(R.id.viewsN);
			viewsLabel = (TextView)v.findViewById(R.id.viewsL);
			bounty = (TextView)v.findViewById(R.id.bounty);
			tags = (LinearLayout)v.findViewById(R.id.tags);
		}
	}

	public int getCount() {
		return questions.size();
	}

	public Object getItem(int position) {
		return questions.get(position);
	}

	public long getItemId(int position) {
		return questions.get(position).getPostId();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Question tmp = questions.get(position);
		View row = convertView;
		TextView tagView;
		ViewHolder viewHolder;
		
		if(row == null) {
			row = inflater.inflate(R.layout.item_question, null);
			viewHolder = new ViewHolder(row);
			row.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) row.getTag();
		}
		
		viewHolder.title.setText(tmp.getTitle());

		viewHolder.votes.setText(String.valueOf(tmp.getScore()));
		if (tmp.getScore() > 1) viewHolder.votesLabel.setText(R.string.votes);
		else viewHolder.votesLabel.setText(R.string.vote);
		
		viewHolder.answers.setText(String.valueOf(tmp.getAnswerCount()));
		if (tmp.getAnswerCount() > 1) viewHolder.answersLabel.setText(R.string.answers);
		else viewHolder.answersLabel.setText(R.string.answer);
		
		viewHolder.views.setText(String.valueOf(tmp.getViewCount()));
		if (tmp.getViewCount() > 1) viewHolder.viewsLabel.setText(R.string.views);
		else viewHolder.viewsLabel.setText(R.string.view);
		
		viewHolder.bounty.setVisibility(View.GONE);
		if (tmp.getBountyAmount() > 0 && new Date(tmp.getBountyClosesDate()).before(new Date())) {
			viewHolder.bounty.setText("+" + String.valueOf(tmp.getBountyAmount()));
			viewHolder.bounty.setVisibility(View.VISIBLE);
		}
		
		viewHolder.tags.removeAllViews();
		for (String tag: tmp.getTags()) {
			tagView = (TextView)inflater.inflate(R.layout.tag, null);
			tagView.setText(tag);
			tagView.setOnClickListener(tagClickListener);
			viewHolder.tags.addView(tagView, tagLayout);
		}
		
		if (tmp.getAnswerCount() == 0) {
			viewHolder.answers.setBackgroundResource(R.color.no_answers_bg);
			viewHolder.answersLabel.setBackgroundResource(R.color.no_answers_bg);
			viewHolder.answers.setTextColor(resources.getColor(R.color.no_answers_text));
			viewHolder.answersLabel.setTextColor(resources.getColor(R.color.no_answers_text));
		}else{
			viewHolder.answers.setBackgroundResource(R.color.some_answers_bg);
			viewHolder.answersLabel.setBackgroundResource(R.color.some_answers_bg);
			if (tmp.getAcceptedAnswerId() > 0) {
				viewHolder.answers.setTextColor(resources.getColor(R.color.answer_accepted_text));
				viewHolder.answersLabel.setTextColor(resources.getColor(R.color.answer_accepted_text));
			}else{
				viewHolder.answers.setTextColor(resources.getColor(R.color.some_answers_text));
				viewHolder.answersLabel.setTextColor(resources.getColor(R.color.some_answers_text));
			}
		}
		
		return row;
	}
	
}
